#include "form_pickup.h"
#include "ui_form_pickup.h"

#include <QDebug>
#include <QStringList>
#include <QMessageBox>
#include "config.h"

extern QString LOGIN;
extern QString DELIVERY;
extern QString PICKUP;
extern QString STATUS;
extern QString BIN_NUM;
extern QString ServerIp;
extern QString Port;


Form_pickup::Form_pickup(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form_pickup)
{
    ui->setupUi(this);

    count_sure = 1;

    socket = new QTcpSocket(this);

    connect(socket,SIGNAL(connected()),this,SLOT(connectedSlot()));

    connect(socket,SIGNAL(readyRead()),this,SLOT(read_pickup_Slot()));

}

Form_pickup::~Form_pickup()
{
    delete ui;
}

void Form_pickup::clearall()
{
    ui->first->setText("");
    ui->again->setText("");
    count_sure = 1;
}

void Form_pickup::connectedSlot()
{
    qDebug() << "connected success";
    send_pickup_request();
    qDebug() << "send pickup";
}

int Form_pickup::get_info(QString recv)
{
    if(recv.length() == 0)
        return -1;
    QStringList list;
    list = recv.split("#");
    qDebug() << "list->length()" << list.length();

    int i;
    for(i = 0; i < list.length(); i++)
    {
        qDebug() << "list->at(" << i << "):" << list.at(i);
    }
    QString tmp = list.last();
    QStringList list2;
    list2 = tmp.split("\r");
    last = list2.first();
    qDebug() << "getinfo" << last;
    return 0;
}

void Form_pickup::read_pickup_Slot()
{

    QByteArray arr;
    QString recv;

    arr.clear();
    arr = socket->readAll();
    recv = QString(arr);
    get_info(recv);


    if(last >= "0")
    {
        qDebug() << "send / open lock";
        QString info = last + " box is open, please fetch pack!";
        if(last.toInt() == 0)
        {
            LED1_ON;
            qDebug()<<"led1 on";
        }
        else
        {
            LED2_ON;
            qDebug()<<"led2 on";
        }
        int choose;
        choose = QMessageBox::information(this, "pickup", info);
        if(choose == QMessageBox::Ok)
        {
            LED1_OFF;
            LED2_OFF;
        }
        else if(choose == QMessageBox::NoButton)
        {
            LED1_OFF;
            LED2_OFF;
        }
    }
    else
    {
        QMessageBox::warning(this, "send", "zero box");
        qDebug() << "send failed";
    }

}

void Form_pickup::send_pickup_request()
{

    QString PHONE_CLIENT = ui->first->text();
    QString AUTH_CODE = ui->again->text();

    QString send;
    send.sprintf("%s#%s#%s#%s#", PICKUP.toStdString().data(), BIN_NUM.toStdString().data()\
                 , PHONE_CLIENT.toStdString().data(), AUTH_CODE.toStdString().data());

    qDebug() << send;
    QByteArray arr;
    arr.clear();
    arr.append(send);
    socket->write(arr);

    qDebug() << send;

    clearall();

}
void Form_pickup::on_enter_clicked()
{
    count_sure++;
    if(count_sure >= 3)
    {
        if(ui->first->text().isEmpty()||ui->again->text().isEmpty())
        {
            count_sure = 1;
            return;
        }
        qDebug() << "order num and auth code, rts";

        socket->abort();
        socket->connectToHost(ServerIp, 8888);

    }

}

void Form_pickup::on_btn_rewrite_clicked()
{
    clearall();
}

void Form_pickup::on_btn_del_clicked()
{
    if(count_sure == 1)
    {
        QString str = ui->first->text();
        int len = str.length();
        QString tmp = str.mid(0, len-1);
        ui->first->setText(tmp);
    }
    if(count_sure == 2)
    {
        QString str = ui->again->text();
        int len = str.length();
        QString tmp = str.mid(0, len-1);
        ui->again->setText(tmp);
    }

}

void Form_pickup::on_btn_ret_clicked()
{
    emit return_welcome();
    clearall();
}

void Form_pickup::on_btn1_clicked()
{

    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("1"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("1"));
}

void Form_pickup::on_btn2_clicked()
{

    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("2"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("2"));
}

void Form_pickup::on_btn3_clicked()
{

    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("3"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("3"));
}

void Form_pickup::on_btn4_clicked()
{

    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("4"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("4"));
}

void Form_pickup::on_btn5_clicked()
{

    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("5"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("5"));
}

void Form_pickup::on_btn6_clicked()
{

    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("6"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("6"));
}

void Form_pickup::on_btn7_clicked()
{

    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("7"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("7"));
}

void Form_pickup::on_btn8_clicked()
{

    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("8"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("8"));
}

void Form_pickup::on_btn9_clicked()
{
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("9"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("9"));
}

void Form_pickup::on_btn0_clicked()
{
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("0"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("0"));
}

void Form_pickup::on_q_clicked()
{
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("q"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("q"));
}

void Form_pickup::on_w_clicked()
{
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("w"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("w"));
}

void Form_pickup::on_e_clicked()
{
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("e"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("e"));
}

void Form_pickup::on_r_clicked()
{
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("r"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("r"));
}

void Form_pickup::on_t_clicked()
{
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("t"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("t"));
}

void Form_pickup::on_y_clicked()
{
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("y"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("y"));
}

void Form_pickup::on_u_clicked()
{
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("u"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("u"));
}

void Form_pickup::on_i_clicked()
{
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("i"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("i"));
}

void Form_pickup::on_o_clicked()
{
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("o"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("o"));
}

void Form_pickup::on_p_clicked()
{
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("p"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("p"));
}

void Form_pickup::on_a_clicked()
{
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("a"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("a"));
}

void Form_pickup::on_s_clicked()
{
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("s"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("s"));
}

void Form_pickup::on_d_clicked()
{
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("d"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("d"));
}

void Form_pickup::on_f_clicked()
{
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("f"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("f"));
}

void Form_pickup::on_g_clicked()
{
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("g"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("g"));
}

void Form_pickup::on_h_clicked()
{
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("h"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("h"));
}

void Form_pickup::on_j_clicked()
{
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("j"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("j"));
}

void Form_pickup::on_k_clicked()
{
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("k"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("k"));
}

void Form_pickup::on_l_clicked()
{
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("l"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("l"));
}

void Form_pickup::on_z_clicked()
{
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("z"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("z"));
}

void Form_pickup::on_x_clicked()
{
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("x"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("x"));
}

void Form_pickup::on_c_clicked()
{
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("c"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("c"));
}

void Form_pickup::on_v_clicked()
{
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("v"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("v"));
}

void Form_pickup::on_b_clicked()
{
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("b"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("b"));
}

void Form_pickup::on_n_clicked()
{
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("n"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("n"));
}

void Form_pickup::on_m_clicked()
{
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("m"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("m"));
}
